﻿
Partial Class WebForm_ManageOrganization
    Inherits BasePage

    Protected Sub trOrganization_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles trOrganization.SelectedNodeChanged
    End Sub

    Protected Sub ContextMenu1_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles ContextMenu1.MenuItemClick

        If e.Item.Value = "Add" Then
            Me.AddOrganization1.Show(Me.OrganizationID)
            Me.TabContainer1.ActiveTab = Me.AddTabPanel
        ElseIf e.Item.Value = "AddInternal" Then
            Me.AddOrganization1.Show(Me.OrganizationID)
            Me.TabContainer1.ActiveTab = Me.AddTabPanel
        ElseIf e.Item.Value = "Edit" Then
            Me.AddOrganization1.ShowEdit(Me.OrganizationID)
            Me.TabContainer1.ActiveTab = Me.AddTabPanel
        ElseIf e.Item.Value = "Disable" Then
            Me.ChangeActiveStatus("N")
            Me.BindOrganization()
        ElseIf e.Item.Value = "Enable" Then
            Me.ChangeActiveStatus("Y")
            Me.BindOrganization()
        ElseIf e.Item.Value = "Cut" Then
            Me.Cutting = True
            Me.CuttedID = Me.OrganizationID
        ElseIf e.Item.Value = "Paste" Then
            If Me.Cutting = False Then
                ShowMessageForAjax(btnQuery, Me.GetLocalResourceObject("None"))
            Else
                Me.Cutting = False
                Me.ChangeParent(Me.trOrganization.SelectedNode.Value)
                Me.BindOrganization()
            End If
        End If
    End Sub

    Protected Sub AddOrganization1_OrganizationAdded(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddOrganization1.OrganizationAdded
        Me.TabContainer1.ActiveTab = Me.SearchTabPanel
        Me.AddData()
    End Sub

    Protected Sub AddOrganization1_OrganizationUpdated(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddOrganization1.OrganizationUpdated
        Me.TabContainer1.ActiveTab = Me.SearchTabPanel
        Me.UpdateData()
    End Sub

    Protected Sub AddOrganization1_Saved(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddOrganization1.Saved
        Me.TabContainer1.ActiveTab = Me.SearchTabPanel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim cssSt As String = "crtCSS('" + Me.ResolveUrl("~/css/" + Session("Theme").ToString + "/WebForm.css") + "');"
        ScriptManager.RegisterStartupScript(Me.Page, GetType(Page), "crtCss", cssSt, True)

        If Not IsPostBack Then
            Me.BindOrganization()
        End If
    End Sub

    Protected Sub ddlActive_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlActive.SelectedIndexChanged
        Me.BindOrganization()
    End Sub

End Class

Partial Class WebForm_ManageOrganization
    Private Function AddData() As Boolean
        Me.BindOrganization()
        Return True
        Dim node As New My.MyTreeNode

        node.MyText = Me.AddOrganization1.OrganizationName
        node.Value = Me.AddOrganization1.OrganizationID

        Me.trOrganization.SelectedNode.ChildNodes.Add(node)

    End Function

    Private Function UpdateData() As Boolean
        Me.BindOrganization()

    End Function

    ''' <summary>
    ''' 取得停用資料,以parent排序
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetNotActive() As Data.DataTable

        Dim bbl As WorkFlow.BBL.OrgBBL = New WorkFlow.BBL.OrgBBL()
        Return bbl.GetNotActive(dbName)
    End Function
    Private Sub BindOrganization()

        Me.trOrganization.Nodes.Clear()

        Dim node As New My.MyTreeNode

        node.MyText = "Root"
        node.Value = "null"

        Me.trOrganization.Nodes.Add(node)

        Me.BindOrganization(node)

        Me.trOrganization.CollapseAll()

    End Sub

    Private Sub BindOrganization(ByVal node As TreeNode)


        Dim bbl As WorkFlow.BBL.OrgBBL = New WorkFlow.BBL.OrgBBL()


        Dim parent As String = node.Value

        For Each dr As Data.DataRow In bbl.GetOrgByParent(parent, ddlActive.SelectedValue, dbName).Rows
            Dim child As New My.MyTreeNode()


            Select Case (GetUserLanguage().ToLower)
                Case "en-us"
                    child.MyText = dr("OrgEngName")
                Case "zh-cn"
                    child.MyText = dr("OrgChnName")
                Case Else
                    child.MyText = dr("OrganizationName")
            End Select

            child.Value = dr("OrganizationID")

            If dr("Active") = "N" Then
                child.MyText = "<b>" + Me.GetLocalResourceObject("STOP") + "</b> " + dr("OrganizationName")
            End If

            node.ChildNodes.Add(child)
            Me.BindOrganization(child)
        Next

    End Sub

    Private Function ChangeActiveStatus(ByVal status As String) As Boolean
      
        Dim bbl As WorkFlow.BBL.OrgBBL = New WorkFlow.BBL.OrgBBL()
        bbl.UpdateActiveStatus(status, Me.OrganizationID, dbName)
    End Function

    Private Function ChangeParent(ByVal parentID As String) As Boolean

        Dim bbl As WorkFlow.BBL.OrgBBL = New WorkFlow.BBL.OrgBBL()
        bbl.ChangeParent(parentID, Me.CuttedID, dbName)
        clsAuth.BuildOrganizationCode(dbName)


    End Function

    Private ReadOnly Property OrganizationID() As String
        Get
            Return Me.trOrganization.SelectedNode.Value
        End Get
    End Property

    Private Property Cutting() As Boolean
        Get
            Return ViewState("Cutting")
        End Get
        Set(ByVal value As Boolean)
            ViewState("Cutting") = value
        End Set
    End Property

    Private Property CuttedID() As String
        Get
            Return ViewState("CuttedID")
        End Get
        Set(ByVal value As String)
            ViewState("CuttedID") = value
        End Set
    End Property
End Class
